package cn.codenest.rabbitmqbootconsumer.util.impl;

import cn.codenest.rabbitmqbootconsumer.util.RbmqService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author ：Hyman
 * @date ：Created in 2021/7/6 19:33
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class RbmqServiceImpl implements RbmqService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    //@RabbitListener(queues ={"TestDirectQueue"} )
    public void consumer(String msg) {
        System.out.println("接受到的消息是：" + msg);
    }

    @Override
    @RabbitListener(containerFactory = "rabbitListenerContainerFactory", bindings = @QueueBinding(value = @Queue(value = "TestDirectQueue", durable = "true"),
            exchange = @Exchange(name = "TestDirectQueue", durable = "true", type = "direct")))
    public void consumer(@Payload Message message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
        System.out.println("接受到的消息是：" + message.toString());
        channel.basicAck(deliveryTag,false);
        System.out.print("这里是接收者1答应消息： ");
        System.out.println("SYS_TOPIC_ORDER_CALCULATE_ZZ_FEE process1  : " + message);
    }

}
